package com.android.hedgehog.puzzles;

import java.util.List;

import android.graphics.Color;

import com.android.hedgehog.Assets;
import com.android.hedgehog.framework.Game;
import com.android.hedgehog.framework.Graphics;
import com.android.hedgehog.framework.Input.TouchEvent;

public class CardMatchScreen2 extends PuzzleScreen {
	enum GameState {
		Start, Play, BadMatch, Finish
	}

	CardField deck;
	int difficulty;
	Graphics graphics;
	GameState state;
	double totalTime;
	double resetTime;
	String resultMsg;
	String testMsg;// test

	public CardMatchScreen2(Game game, int gameType, int difficulty, int instNumber) {
		this.difficulty = difficulty;
		this.gameType = gameType;
		instanceNumber = instNumber;
		graphics = game.getGraphics();
		if (instanceNumber < 2)
			state = GameState.Start;
		else
			state = GameState.Play;
		deck = new CardField(difficulty);
	}

	@Override
	public void update(List<TouchEvent> touchEvents, double deltaTime) {
		totalTime += deltaTime;
		if (state == GameState.Start)
			updateStart();
		if (state == GameState.Play)
			updateCard(touchEvents);
		if (state == GameState.BadMatch)
			updateBadMatch(deltaTime);
		if (state == GameState.Finish) {
			updateFinish(deltaTime);
		}

	}

	private void updateFinish(double deltaTime) {
		resetTime += deltaTime;
		if (resetTime > 3.6)
			instanceOver = true;
	}

	private void updateBadMatch(double deltaTime) {
		resetTime += deltaTime;
		if (resetTime > .7) {
			resetTime = 0.0;
			deck.reset();
			state = GameState.Play;
		}
	}

	private void updateCard(List<TouchEvent> touchEvents) {

		int len = touchEvents.size();
		for (int i = 0; i < len; i++) {// FOR
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) {
				if (deck.totalFlipped < 2)
					deck.flip(event.x, event.y);
			}
		}
		if (deck.totalFlipped > 1) {// ***************2CARDS FLIPPED
			deck.totalTurns--;
			if (deck.isMatch()) {// ************GOOD MATCH
				if (isGameOver()) {
					state = GameState.Finish;
				} else {
					deck.reset();
				}
			}// ********************************GOOD MATCH

			else {// **********************BAD MATCH
				if (isGameOver()) {
					state = GameState.Finish;
				} else {
					state = GameState.BadMatch;
				}
			}// ***************************BAD MATCH

			deck.totalFlipped = 0;
		}// *******************************************2CARDS FLIPPED

	}

	private boolean isGameOver() {

		if (deck.isComplete()) {// SUCCESS!
			instanceScore += difficulty;
			success = true;
			return true;
		}
		if (deck.totalTurns < 1) {
			return true;
		}
		return false;
	}

	private void updateStart() {
		if (totalTime >= 2.0)
			state = GameState.Play;
	}

	@Override
	public void present() {
		if (state == GameState.Start)
			presentStart();
		if (state != GameState.Start) {
			presentPlay();
		}

	}

	private void presentPlay() {
		if (state != GameState.Finish) {
			graphics.drawText("Turns Left: " + deck.totalTurns, 160, 66, 24, Color.WHITE);
		} else {// GAMEOVER
			if (success) {
				graphics.drawText("Well Done", 160, 56, 24, Color.GREEN);
				if (gameType == 1)
					graphics.drawText("+" + instanceScore, 160, 74, 15, Color.GREEN);
			} else {
				graphics.drawText("Out of Turns!", 160, 66, 30, Color.RED);
			}

		}// GAMEOVER

		for (int i = 0; i < deck.numberOfCards; i++) {
			if (deck.cards[i].flipped || deck.cards[i].matched) {
				graphics.drawPixmap(Assets.cards, deck.cards[i].x, deck.cards[i].y, deck.cards[i].value * 50,
						0, 50, 70);
			} else {
				graphics.drawUniversalRect(deck.cards[i].x, deck.cards[i].y, 50, 70, false, true, 1, 255,
						Color.GRAY);
				graphics.drawUniversalRect(deck.cards[i].x, deck.cards[i].y, 50, 70, false, false, 4, 255,
						Color.DKGRAY);
			}

		}
	}

	private void presentStart() {
		graphics.drawSpecialText("Match Pairs", 160, 240, 24, 2, Color.BLUE);
		graphics.drawSpecialText("of Identical Cards", 160, 265, 24, 2, Color.BLUE);
	}

}
